package com.itheima.controller;

import com.itheima.entity.Emp;
import com.itheima.entity.EmpQueryParam;
import com.itheima.entity.PageBean;
import com.itheima.entity.Result;
import com.itheima.mapper.EmpMapper;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;

    @GetMapping
    public Result page(/*@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Integer gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end*/
    EmpQueryParam empQueryParam
    ) {
//        log.info("分页查询员工信息，当前页码：{}，每页记录数：{}", page, pageSize);
//        log.info("员工姓名：{}，员工性别：{}，开始日期：{}，结束日期：{}", name, gender, begin, end);
//        PageBean pageBean = empService.page(page, pageSize);
        log.info("员工查询参数：{}", empQueryParam);
//        PageBean pageBean = null;
        PageBean pageBean = empService.page(empQueryParam);
        return Result.success(pageBean);
    }

    /**
     * 新增员工
     * @param emp
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Emp emp) {
        log.info("新增员工，员工信息：{}", emp);
        empService.save(emp);
        return Result.success();
    }

    /**
     * 删除员工
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam("ids")List<Integer> ids) {
        log.info("删除员工，员工编号：{}", ids);
        empService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 查询员工信息，根据员工编号
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable("id") Integer id) {
        log.info("查询员工信息，员工编号：{}", id);
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    /**
     * 更新员工信息
     * @param emp
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("更新员工信息：{}", emp);
        empService.update(emp);
        return Result.success();
    }

    /**
     * 查询所有员工信息
     * @return
     */
    @GetMapping("/list")
    public Result list(){
        List<Emp> list = empService.list();
        return Result.success(list);
    }

}
